// base64转文件流
import { infoClient } from "./aliOss";
function dataURLtoFile(dataurl, filename = "file") {
    let arr = dataurl.split(",");
    let mime = arr[0].match(/:(.*?);/)[1];
    let suffix = mime.split("/")[1];
    let bstr = atob(arr[1]);
    let n = bstr.length;
    let u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], `${filename}.${suffix}`, {
        type: mime,
    });
}
//异步转同步
export function GetAppImages(data) {
    return new Promise(function (resolve, reject) {
        let f1 = function (i) {
            return new Promise(function (resolve, reject) {
                var files = dataURLtoFile(
                    data[i],
                    "goodsRelease/goodsUrl/" + new Date().getTime()
                );
                infoClient()
                    .put(files.name, files)
                    .then((res) => {
                        if (res.res.status === 200) {
                            resolve({
                                url: res.url,
                            });
                        } else {
                            resolve({
                                url: '',
                                status: "failed",
                                message: "上传失败",
                            });
                        }
                    })
                    .catch((err) => {
                        resolve({
                            url: '',
                            status: "failed",
                            message: "上传失败",
                        });
                    });
            });
        };
        let loopNum = 0;
        const list = [];
        let asyncControl = function () {
            if (loopNum < data.length) {
                f1(loopNum).then((res) => {
                    loopNum++;
                    list.push(res);
                    asyncControl();
                });
            } else {
                resolve(list);
            }
        };
        asyncControl();
    });
}